home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 1 / Amiga Tools.iso / disk-tools / hd-tools / scsi_tape / util / readpage.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-06  |  3.2 KB  |  181 lines

  1.  
  2. /*
  3.  *  READPAGE.C
  4.  *
  5.  */
  6.  
  7. #include <exec/types.h>
  8. #include <exec/ports.h>
  9. #include <exec/io.h>
  10. #include <exec/memory.h>
  11. #include <devices/scsidisk.h>
  12. #include <libraries/dos.h>
  13. #include <libraries/dosextens.h>
  14. #include <libraries/filehandler.h>
  15. #include <clib/exec_protos.h>
  16. #include <clib/alib_protos.h>
  17.  
  18. typedef unsigned char ubyte;
  19.  
  20. typedef struct MsgPort    MsgPort;
  21. typedef struct IOStdReq IOStdReq;
  22. typedef struct SCSICmd    SCSICmd;
  23.  
  24. typedef struct SCSIReq {
  25.     IOStdReq sr_Req;
  26.     SCSICmd  sr_Cmd;
  27. } SCSIReq;
  28.  
  29. MsgPort *IoSink;
  30. SCSIReq Ios;
  31.  
  32. char *ErrorAry[] = {
  33.     "No Sense",
  34.     "Recovered Error",
  35.     "Not Ready",
  36.     "Medium Error",
  37.     "Hardware Error",
  38.     "Illegal Request",
  39.     "Unit Attention",
  40.     "Data Protect",
  41.     "Blank Check",
  42.     "Vendor Specific",
  43.     "Copy Aborted",
  44.     "Aborted Command",
  45.     "Equal (search_data)",
  46.     "Volume Overflow",
  47.     "MisCompare",
  48.     "Reserved"
  49. };
  50.  
  51. void myexit(void);
  52.  
  53. main(ac, av)
  54. char *av[];
  55. {
  56.     atexit(myexit);
  57.  
  58.     IoSink = CreatePort(NULL, 0);
  59.  
  60.     if (SCSIOpen("scsi.device", 4) < 0) {
  61.     puts("Unable to open scsi.device");
  62.     exit(20);
  63.     }
  64.     if (ac == 1) {
  65.     short i;
  66.     for (i = 0; i < 0x40; ++i)
  67.         ReadPage(i);
  68.     } else {
  69.     ReadPage(strtol(av[1], NULL, 0));
  70.     }
  71.     return(0);
  72. }
  73.  
  74. ReadPage(pageNo)
  75. short pageNo;
  76. {
  77.     __aligned char modeSenseCmd[] = { 0x1A, 0x00, pageNo, 0x00, 0xFF, 0x00 };
  78.     unsigned char buf[512];
  79.     int error;
  80.     long len;
  81.  
  82.     if (error = DoSCSI(modeSenseCmd, buf, sizeof(buf), SCSIF_READ, &len)) {
  83.     char *errorStr = (error > 0 && error < 16) ? ErrorAry[error] : "?";
  84.     printf("error code %2d (0x%02x) %s\n", error, error, errorStr);
  85.     } else {
  86.     short i;
  87.  
  88.     for (i = 0; i < len; ++i) {
  89.         if (i && (i & 7) == 0)
  90.         puts("");
  91.         printf(" %02x", buf[i]);
  92.     }
  93.     puts("");
  94.     }
  95.     return(error);
  96. }
  97.  
  98. void
  99. myexit(void)
  100. {
  101.     SCSIClose();
  102.     if (IoSink)
  103.     DeletePort(IoSink);
  104. }
  105.  
  106.  
  107. int
  108. SCSIOpen(devName, unitNo)
  109. char *devName;
  110. long unitNo;
  111. {
  112.     int error;
  113.  
  114.     Ios.sr_Req.io_Message.mn_ReplyPort = IoSink;
  115.     if (error = OpenDevice(devName, unitNo, &Ios.sr_Req, 0))
  116.     error = -1;
  117.     return(error);
  118. }
  119.  
  120. int
  121. SCSIClose()
  122. {
  123.     if (Ios.sr_Req.io_Device)
  124.     CloseDevice(&Ios);
  125. }
  126.  
  127. int
  128. DoSCSI(cmd, buf, bytes, flags, len)
  129. char *cmd;
  130. char *buf;
  131. long bytes;
  132. ubyte flags;
  133. long *len;
  134. {
  135.     static ubyte senseCmd[] = { 0x03, 0x00, 0x00, 0x00, 20, 0 };
  136.     SCSIReq *ios = &Ios;
  137.     char senseBuf[20];
  138.     int error;
  139.  
  140.     ios->sr_Cmd.scsi_Command   = cmd;
  141.     ios->sr_Cmd.scsi_CmdLength = 6;
  142.     ios->sr_Cmd.scsi_Data   = buf;
  143.     ios->sr_Cmd.scsi_Length = bytes;
  144.     ios->sr_Cmd.scsi_Flags  = flags;
  145.  
  146.     ios->sr_Req.io_Command = 28;
  147.     ios->sr_Req.io_Data    = &ios->sr_Cmd;
  148.     ios->sr_Req.io_Length  = sizeof(ios->sr_Cmd);
  149.  
  150.     error = DoIO(&ios->sr_Req);
  151.     if (len)
  152.     *len = ios->sr_Cmd.scsi_Actual;
  153.  
  154.     switch(error) {
  155.     case 0:
  156.     break;
  157.     case HFERR_BadStatus:
  158.     ios->sr_Cmd.scsi_Command   = senseCmd;
  159.     ios->sr_Cmd.scsi_CmdLength = 6;
  160.     ios->sr_Cmd.scsi_Data    = (UWORD *)senseBuf;
  161.     ios->sr_Cmd.scsi_Length = sizeof(senseBuf);
  162.     ios->sr_Cmd.scsi_Flags    = SCSIF_READ;
  163.  
  164.     if (DoIO(&ios->sr_Req)) {
  165.         error = -256;
  166.         break;
  167.     }
  168.     if (ios->sr_Cmd.scsi_Actual < 4) {
  169.         error = -257;
  170.         break;
  171.     }
  172.     error = senseBuf[2];
  173.     break;
  174.     default:
  175.     error = -error;
  176.     break;
  177.     }
  178.     return(error);
  179. }
  180.  
  181.